package com.app.adnroid.sichuanxiaoke.cn.xiaokebusiness.Utils;


import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

import com.app.adnroid.sichuanxiaoke.cn.xiaokebusiness.activity.GoPayAppActivity;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;


/**
 * 支付相关SDK
 *
 * @author yekangning
 */
public class PaySDK {

    //在调用toPayApp()方法的Activity中，重写onActivityResult()方法时调用
    public static final int REQ_PAY = 1;    //请求码
    public static final int RESULT_OK = 2;    //结果码
    public static Map<String, Object> map = new HashMap<String, Object>();

    private PayListener payListenr = null;
    private Context context = null;

    public PaySDK(Context context) {
        this.context = context;
    }

    /**
     * 调用此方法跳转到支付App
     *
     * @param context     当前跳转的activity
     * @param packageName 目标App包名
     * @param className   目标activity全名(包名+类名)
     * @param map        传递的数据
     */
    public boolean toPayApp(Activity context, String packageName, String className, Map<String, Object> map) {
        if (!check())
            return false;
        String contextString = context.toString();
        String clsName = contextString.substring(0, contextString.indexOf("@"));
        Intent intent = new Intent(context, GoPayAppActivity.class);
        Bundle bundle = new Bundle();
        //bundle.putString("packageName", packageName);
        //bundle.putString("className", className);
        bundle.putSerializable("data", (Serializable) map);
        bundle.putString("clsName", clsName);
        intent.putExtras(bundle);
        context.startActivity(intent);
        context.finish();
        return true;
    }


    public void sendSuccess(Map<String, Object> message) {
        this.payListenr.onSuccess(message);
    }

    public void sendFail(String message) {
        this.payListenr.onFail(message);
    }

    public void setPayListener(PayListener payListener) {
        this.payListenr = payListener;
    }

    public boolean check() {
        if (this.payListenr == null)
            return false;
        return true;
    }


    /**
     * 调用此方法获得传递的数据
     *
     * @param context 当前activity
     * @return 获得的数据
     */
    @SuppressWarnings("unchecked")
    public static Map<String, Object> getData(Activity context) {

        return (Map<String, Object>) context.getIntent().getSerializableExtra("data");
    }

    /**
     * 调用此方法回传数据
     *
     * @param context 当前activity
     * @param map    回传的数据
     */
    public static void returnData(Activity context, Map<String, Object> map) {
        Intent intent1 = new Intent();
        Bundle bundle = new Bundle();
        bundle.putSerializable("result", (Serializable) map);
        intent1.putExtras(bundle);
        context.setResult(Activity.RESULT_OK, intent1);
        context.finish();
    }

}
